package com.tyut.homework.controller;


import com.tyut.homework.pojo.Student;
import com.tyut.homework.response.ServerResponse;
import com.tyut.homework.service.StudentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(description = "Student")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @ApiOperation("查询某个学生信息")
    @GetMapping("/findById/{id}")
    public ServerResponse findById(@PathVariable Integer id){
        Student student = studentService.find(id);
        return ServerResponse.createBySuccess(student);
    }

    @ApiOperation("查看学生分页信息")
    @GetMapping("/findAll/{pageNum}/{pageLimit}")
    public ServerResponse findAll(@PathVariable Integer pageNum,
                                  @PathVariable Integer pageLimit){
        return ServerResponse.createBySuccess(studentService.getPage(pageNum, pageLimit));
    }

    @ApiOperation("添加学生")
    @PostMapping("/add")
    public ServerResponse add(Student student){
        return ServerResponse.createBySuccess("添加成功",studentService.save(student));
    }

    @ApiOperation("更新学生信息")
    @PostMapping("/update")
    public ServerResponse update(Student student){
        return  ServerResponse.createBySuccess(studentService.update(student));
    }

    @ApiOperation("删除学生")
    @GetMapping("/delete/{id}")
    public ServerResponse delete(@PathVariable Integer id){
        return ServerResponse.createBySuccess("删除成功!",studentService.delete(id));
    }

    @ApiOperation("查询各个班级人数")
    @GetMapping("/findAllClassInfo")
    public ServerResponse findAllClassInfo(){
        return null;
    }

    @ApiOperation("查询某个班级人数")
    @GetMapping("/findClassInfo/{clazz}")
    public ServerResponse findClassInfo(@PathVariable String clazz){
        return null;
    }

    @ApiOperation("批量删除")
    @PostMapping("/deleteInBatch")
    public ServerResponse deleteInBatch(@RequestParam String [] ids){
        System.out.println(Arrays.toString(ids));
        studentService.deleteInbatch(ids);
        return ServerResponse.createBySuccess();
    }

    @ApiOperation("模糊查询")
    @PostMapping("/findByLike")
    public ServerResponse findByLike(@RequestParam Integer type,
                                     @RequestParam String content){
        List<Student> byLike =  studentService.findByLike(type, content);
        byLike.forEach(System.out::println);
        return ServerResponse.createBySuccess(byLike);
    }


}
